﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using DailyDiet.Models;
using DailyDiet.Models.VO;

namespace DailyDiet.Services.Impl
{
    public class DietToDietAndPictureService : IDietToDietAndPictureService {
        private IDietPictureService _dietPictureService;
        /// <summary>
        /// 转换一个
        /// </summary>
        /// <param name="diet">待转换的diet</param>
        /// <returns></returns>
        public async Task<DietAndPicture> TranslateOneAsync(Diet diet) {
            var dietAndPicture = new DietAndPicture();
            dietAndPicture.Diet = diet;
            dietAndPicture.ImageBytes =
                await _dietPictureService.GetOneDietPictureAsync(diet);
            return dietAndPicture;
        }

        /// <summary>
        /// 转换一个列表的
        /// </summary>
        /// <typeparam name="diets">待转换的diet集合</typeparam>
        /// <returns></returns>
        public async Task<List<DietAndPicture>> TranslateListAsync(List<Diet> diets) {
            List<DietAndPicture> dietAndPictures = new List<DietAndPicture>();
            Dictionary<int, byte[]> idBytesMap =
                await _dietPictureService.GetDietPicturesAsync(diets);
            foreach (var diet in diets) {
                dietAndPictures.Add(new DietAndPicture() {
                    Diet = diet,
                    ImageBytes = idBytesMap[diet.Id]
                });
            }

            return dietAndPictures;
        }

        public DietToDietAndPictureService(IDietPictureService dietPictureService) {
            _dietPictureService = dietPictureService;
        }
    }
}
